package cc.booku.owlbox.UserSettingsPage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;
import cc.booku.owlbox.VideoPlayer.PlayerActivity;
import cc.booku.owlbox.loadSqlite.DatabaseHelper;
import cc.booku.owlbox.loadSqlite.collectDate;

public class loadDataActivity extends AppCompatActivity {

    private Toolbar Toolbar;
    private NoScrollListView userdata_list;
    private List<loadData_C> loadData_cList = new ArrayList<loadData_C>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_data);


        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        userdata_list = (NoScrollListView) findViewById(R.id.userdata_list);

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String FileName = bundle.getString("FileName");
        String FilePath = bundle.getString("FilePath");
        LoadToolbar(FileName);

        // Log.d("LoadImage", "img=====" + FilePath+FileName);

        //读取的文件数据：FileoOperations.readFromSDFile(this,SpecifiedFile)
        // String str = FileoOperations.readFromSDFile(this, FilePath+FileName);
        // Log.d("LoadImage", "img=====" + str);
        try {
            //尝试解析代码
            FastJson_analysis(FilePath + FileName);
        } catch (Exception e) {
            //异常处理
            Toast.makeText(loadDataActivity.this, "数据解析错误", Toast.LENGTH_SHORT).show();
        }


    }


    private void LoadToolbar(String sqlTitle) {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle(sqlTitle);

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });

    }


    private loadData_C loadDataC;

    //读取文件数据
    private void FastJson_analysis(String str) {

        String SpecifiedFile = FileoOperations.readFromSDFile(this, str);
//使用fastJson解析Json
        List<loadData_C> ld_c = JSON.parseArray(SpecifiedFile.toString(), loadData_C.class);
        for (loadData_C normVideo : ld_c) {
            loadDataC = new loadData_C();


            String vod_score = normVideo.getVod_score();
            String vod_name = normVideo.getVod_name();
            String vod_class = normVideo.getVod_class();
            String vod_add_time = normVideo.getVod_add_time();
            String vod_pic = normVideo.getVod_pic();
            String vod_play_url = normVideo.getVod_play_url();
            //String vod_value_add = normVideo.getVod_value_add();

            String vod_actor = normVideo.getVod_actor();
            String vod_director = normVideo.getVod_director();
            String vod_blurb = normVideo.getVod_blurb();
            String vod_area = normVideo.getVod_area();
            String vod_year = normVideo.getVod_year();
            String vod_lang = normVideo.getVod_lang();
            String vod_id = normVideo.getVod_id();

            loadDataC.setVod_score("评分:" + vod_score);
            loadDataC.setVod_name(vod_name);
            loadDataC.setVod_class(vod_class + "#" + vod_year + "#" + vod_area + "#" + vod_lang + "#" + vod_id);
            loadDataC.setVod_add_time(vod_add_time);
            loadDataC.setVod_pic(vod_pic);
            loadDataC.setVod_pic_url(vod_pic);
            loadDataC.setVod_play_url(vod_play_url);
            loadDataC.setVod_value_add("导演：" + vod_director + "\n演员：" + vod_actor + "\n类型：" + vod_class + "\n上映地区：" + vod_area + "\n上映年份：" + vod_year + "\n语言：" + vod_lang + "$片名：" + vod_name + "\n剧情简介：" + vod_blurb);

            loadData_cList.add(loadDataC);
        }
        Message msg = new Message();
        msg.what = 16;
        handler.sendMessage(msg);//用activity中的handler发送消息


    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {


                case 16:
                    listClickListener((ArrayList<loadData_C>) loadData_cList);
                    break;


            }
        }
    };


    private loadData_A loadDataA;

    public void listClickListener(ArrayList<loadData_C> loadData_cList) {


        loadDataA = new loadData_A(R.layout.loaddata_item, this, loadData_cList);

        userdata_list.setAdapter(loadDataA);
        userdata_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //Toast.makeText(getContext(), "Click ++ item" + i, Toast.LENGTH_SHORT).show();
                final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);
                final TextView vod_score1 = (TextView) view.findViewById(R.id.vod_score);
                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
                final TextView vod_add_time1 = (TextView) view.findViewById(R.id.vod_add_time);
                final TextView vod_play_url1 = (TextView) view.findViewById(R.id.vod_play_url);
                final TextView vod_value_add1 = (TextView) view.findViewById(R.id.vod_value_add);
                final TextView vod_pic_url1 = (TextView) view.findViewById(R.id.vod_pic_url);

                String vod_class2 = vod_class1.getText().toString();
                String vod_score2 = vod_score1.getText().toString();
                String vod_name2 = vod_name1.getText().toString();
                String vod_add_time2 = vod_add_time1.getText().toString();
                String vod_play_url2 = vod_play_url1.getText().toString();
                String vod_value_add2 = vod_value_add1.getText().toString();
                String vod_pic_url2 = vod_pic_url1.getText().toString();

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("vod_class", vod_class2);
                bundle.putString("vod_score", vod_score2);
                bundle.putString("vod_name", vod_name2);
                bundle.putString("vod_add_time", vod_add_time2);
                bundle.putString("vod_play_url", vod_play_url2);
                bundle.putString("vod_value_add", vod_value_add2);
                bundle.putString("vod_pic", vod_pic_url2);
                intent.putExtras(bundle);
                intent.setClass(loadDataActivity.this, PlayerActivity.class);
                startActivity(intent);


            }
        });


    }


//    private loadDataActivity.video_public_Adapter video_public_AdapterA;
//    public void USERcollectDate(ArrayList<loadData_C> loadData_cList) {
//
//
//        video_public_AdapterA = new loadDataActivity.video_public_Adapter(R.layout.loaddata_item, this, loadData_cList);
//
//        userdata_list.setAdapter(video_public_AdapterA);
//        userdata_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//
//                //跳转提示
//               // Toast.makeText(UserDataActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
//                final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);
//                final TextView vod_score1 = (TextView) view.findViewById(R.id.vod_score);
//                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
//                final TextView vod_add_time1 = (TextView) view.findViewById(R.id.vod_add_time);
//                final TextView vod_play_url1 = (TextView) view.findViewById(R.id.vod_play_url);
//                final TextView vod_value_add1 = (TextView) view.findViewById(R.id.vod_value_add);
//
//
//                String vod_class2 = vod_class1.getText().toString();
//                String vod_score2 = vod_score1.getText().toString();
//                String vod_name2 = vod_name1.getText().toString();
//                String vod_add_time2 = vod_add_time1.getText().toString();
//                String vod_play_url2 = vod_play_url1.getText().toString();
//                String vod_value_add2 = vod_value_add1.getText().toString();
//
//
//                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putString("vod_class", vod_class2);
//                bundle.putString("vod_score", vod_score2);
//                bundle.putString("vod_name", vod_name2);
//                bundle.putString("vod_add_time", vod_add_time2);
//                bundle.putString("vod_play_url", vod_play_url2);
//                bundle.putString("vod_value_add", vod_value_add2);
//                intent.putExtras(bundle);
//                intent.setClass(loadDataActivity.this, PlayerActivity.class);
//                startActivity(intent);
//
//            }
//        });
//
//
//
//
//    }

//    class video_public_Adapter extends BaseAdapter implements ListAdapter {
//
//
//        private ArrayList<loadData_C> loadDataCArrayList;
//        private int id;
//        private Context context;
//        private LayoutInflater inflater;
//
//        public video_public_Adapter(int item, Context context, ArrayList<loadData_C> loadDataCArrayList) {
//            this.loadDataCArrayList = loadDataCArrayList;
//            this.context = context;
//            this.id = item;
//            inflater = LayoutInflater.from(context);
//
//        }
//
//        @Override
//        public int getCount() {
//            return loadDataCArrayList.size();
//        }
//
//        @Override
//        public Object getItem(int i) {
//            return loadDataCArrayList.get(i);
//        }
//
//        @Override
//        public long getItemId(int i) {
//            return i;
//        }
//
//        @SuppressLint("WrongConstant")
//        @Override
//
//        public View getView(int i, View view, ViewGroup viewGroup) {
//
//
//
//            TextView vod_score = null;
//            TextView vod_name = null;
//            TextView vod_class = null;
//            TextView vod_add_time = null;
//            ImageView vod_pic = null;
//            TextView vod_play_url = null;
//            TextView vod_value_add = null;
//
//            loadDataActivity.video_public_Adapter.ViewHolder viewHolder;
//            if (view == null) {
//                view = inflater.inflate(id, null);
//
//                vod_score = (TextView) view.findViewById(R.id.vod_score);
//                vod_name = (TextView) view.findViewById(R.id.vod_name);
//                vod_class = (TextView) view.findViewById(R.id.vod_class);
//                vod_add_time = (TextView) view.findViewById(R.id.vod_add_time);
//                vod_value_add = (TextView) view.findViewById(R.id.vod_value_add);
//                vod_play_url = (TextView) view.findViewById(R.id.vod_play_url);
//
//                vod_pic = (ImageView) view.findViewById(R.id.vod_pic);
//
//
//
//                view.setTag(new loadDataActivity.video_public_Adapter.ViewHolder(vod_score, vod_name,vod_class,vod_add_time,vod_value_add,vod_play_url,vod_pic));
//            } else {
//                loadDataActivity.video_public_Adapter.ViewHolder viewHolder1 = (loadDataActivity.video_public_Adapter.ViewHolder) view.getTag(); // 重新获取ViewHolder
//                vod_score = viewHolder1.vod_score;
//                vod_class = viewHolder1.vod_class;
//                vod_name = viewHolder1.vod_name;
//                vod_add_time = viewHolder1.vod_add_time;
//                vod_value_add = viewHolder1.vod_value_add;
//                vod_play_url = viewHolder1.vod_play_url;
//
//
//
//
//            }
//            loadData_C video_public_classListc = (loadData_C) loadDataCArrayList.get(i); // 获取当前项的实例
//
//            //video_public_classListc
//            vod_name.setText(video_public_classListc.getVod_name().toString());//对象为空
//            vod_class.setText(video_public_classListc.getVod_class().toString());
//            vod_score.setText(video_public_classListc.getVod_score().toString());
//            vod_add_time.setText(video_public_classListc.getVod_add_time().toString());
//            vod_value_add.setText(video_public_classListc.getVod_value_add().toString());
//            vod_play_url.setText(video_public_classListc.getVod_play_url().toString());
//            LoadImage(vod_pic, video_public_classListc.getVod_pic());
//
//            return view;
//
//        }
//        private void LoadImage(ImageView img, String path) {
//            //ImageLoader.loadImage(image_url, filmclass.getPimg());
//            //Log.d("LoadImage", "img=====" + path);
//            AsyncTaskImageLoad async = new AsyncTaskImageLoad(img);
//            async.executeOnExecutor(Executors.newCachedThreadPool(), path);//快的多
//            //async.execute(path);
//        }
//
//        private final class ViewHolder {
//            TextView vod_score = null;
//            TextView vod_name = null;
//            TextView vod_class = null;
//            TextView vod_add_time = null;
//            ImageView vod_pic = null;
//            TextView vod_play_url = null;
//            TextView vod_value_add = null;
//
//
//            public ViewHolder(TextView vod_score,TextView vod_name,TextView vod_class,TextView vod_add_time,TextView vod_value_add,TextView vod_play_url,ImageView vod_pic) {
//                this.vod_class = vod_class;
//                this.vod_score = vod_score;
//                this.vod_name = vod_name;
//                this.vod_add_time = vod_add_time;
//                this.vod_play_url = vod_play_url;
//                this.vod_value_add = vod_value_add;
//                this.vod_pic = vod_pic;
//            }
//
//        }
//    }

}
